package cn.middle.application.portal;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.middle.common.result.ApiResponse;
import com.middle.common.result.Page;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.middle.application.entity.DoorUser;
import cn.middle.application.entity.EdgeService;
import cn.middle.application.remote.DoorRemoteApi;
import cn.middle.application.service.DoorUserService;
import cn.middle.application.service.EdgeServiceService;
import cn.middle.application.service.ParkService;

@Controller
@RequestMapping("/doorUser")
public class DoorUserController {
	
	
	@Autowired
	private DoorUserService doorUserService;
	
	@Autowired
	private ParkService parkService;
	
	@Autowired
	private EdgeServiceService edgeService;
	
	@Autowired
	private DoorRemoteApi remoteApi;
	
	
	
	private static final String PREFIX = "business/door/";
	
	@SaCheckPermission("doorUser:list")
	@GetMapping("/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(value="pageSize",defaultValue="10") Integer limit,
			String name,ModelMap modelMap){
		Page<DoorUser> doorUsers = this.doorUserService.listOrName(pageNum, limit, name) ;
	    modelMap.put("name", name);
		modelMap.put("pageInfo", doorUsers);
		return PREFIX + "list";
	}
	
	@SaCheckPermission("doorUser:add")
	@GetMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.put("parks", parkService.getAll());
		return PREFIX + "add";
	}
	
	@SaCheckPermission("doorUser:add")
	@ResponseBody
	@PostMapping("/add")
	public ApiResponse<Boolean> add(@Valid DoorUser user){
		user.setCreateTime(new Date());
		this.doorUserService.save(user);
		this.remoteApi.sendAddUser(user); //同步到边缘服务
		return ApiResponse.successWithData(true);
	}
	
	
//	public DoorUserDto findById(Long id) {
//	     return remoteApi.sendGetUserById(1L);
//	}
	
	@SaCheckPermission("doorUser:delete")
	@ResponseBody
	@GetMapping("/delete")
	public ApiResponse<Boolean> delete(@NotNull(message="用户id不能为空！")Long id){
		
		this.remoteApi.sendGetUserById(id);
		this.remoteApi.sendDeleteUser(id);//同步到边缘服务
		this.doorUserService.deleteById(id);
		return ApiResponse.successWithData(true);
	}
	
	@SaCheckPermission("doorUser:add")
	@ResponseBody
	@GetMapping("/findEdgeServiceByParkId")
	public ApiResponse<List<EdgeService>> findEdgeServiceByParkId(@NotNull(message = "园区ID不能为空！")String parkId){
		List<EdgeService> edgeServices =  edgeService.findByParkId(parkId);
		return ApiResponse.successWithData(edgeServices);
	}
	
	@SaCheckLogin
	@GetMapping("/subscribeDoorEvent")
	@ResponseBody
	public ApiResponse<Boolean> subscribeDoorEvent(int eventKey){
		this.remoteApi.subscribe(eventKey);
		return ApiResponse.successWithData(true);
	}

}
